'use strict';

Vue.component('paginate', VuejsPaginate);
initDatePicker();

function initDatePicker() {
    $.fn.datepicker.languages['zh-CN'] = {
        format: 'yyyy-mm-dd',
        days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
        daysShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
        daysMin: ['日', '一', '二', '三', '四', '五', '六'],
        months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        monthsShort: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        weekStart: 1,
        startView: 0,
        yearFirst: true,
        yearSuffix: '年'
    };

    function getNum(num) {
        return num < 10 ? '0' + num : num;
    }
    Vue.component('date-picker', {
        template: '<input class= "" :value="dateValue"/>',
        props: ['dateValue'],
        mounted: function mounted() {
            var self = this;
            $(this.$el).datepicker({
                autoHide: true,
                language: 'zh-CN'
            }).on('pick.datepicker', function (e) {
                self.$emit('update-date', e.date.getFullYear() + '-' + getNum(e.date.getMonth() + 1) + '-' + getNum(e.date.getDate()));
            });
        }
    });
}

var payPage = new Vue({
    el: '#pay-page',
    data: {
        'urlHeadCommon': 'http://kmb.leehq.com/pc/',
        'payList': [],
        'payOne': {},
        'payWayNum': [],
        'remindInfo': '',
        'isShowPayPage': false
    },
    created: function created() {
        if (sessionStorage.getItem('token')) {
            this.isLogin = true;
        }
        this.payType();
    },

    methods: {
        payType: function payType() {
            var _this = this;

            var token = sessionStorage.getItem('token') || '';
            var telNo = sessionStorage.getItem('phone') || '';
            $.ajax({
                url: this.urlHeadCommon + 'p/pay/query' + '?token=' + token + '&telNo=' + telNo,
                type: 'GET',
                header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
            }).done(function (data) {
                _this.payList = data.data.payList;
                _this.payOne = _this.payList[2];
                _this.isShowPayPage = true;
            });
        },
        closePage: function closePage() {
            $('.pay-page').hide();
        },
        payMenu: function payMenu(index1, index2) {
            $('.pay-munu').siblings().removeClass('active');
            $('.pay-munu').eq(index1 - 1).addClass('active');
            this.payOne = this.payList[index2];
        },
        payWay: function payWay(index) {
            $('.pay-internet').siblings().removeClass('active');
            $('.pay-internet').eq(index).addClass('active');
            this.payWayNum = index;
        },
        rightPay: function rightPay() {
            var _this2 = this;

            if (!this.isLogin) {
                $('.pay-page').hide();
                $('.login-page').show();
                return;
            }
            var token = sessionStorage.getItem('token') || '';
            var telNo = sessionStorage.getItem('phone') || '';
            var data = {
                price: this.payOne.price,
                payGoodsId: this.payOne.id
            };

            if (this.payWayNum === 1) {
                $.ajax({
                    url: this.urlHeadCommon + 'c/wechat/v1.0/genqrcode' + '?token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' },
                    data: data
                }).done(function (data) {
                    _this2.payWXurl = data.data;

                    // new QRCode($('.pay-code'), {
                    //     width : 100,
                    //     height : 100
                    // }).makeCode(data.data);

                });
            } else {
                this.remindInfo = '请选择微信支付';
                $('.remind-info').show();

                var t = setTimeout(function () {
                    $('.remind-info').hide();
                }, 1000);
            }
        },
        closeRemind: function closeRemind() {
            $('.remind-info').hide();
        }
    }
});